package com.w.wx.controller;

import com.alibaba.fastjson.JSONObject;
import com.w.wx.domain.*;
import com.w.wx.mapper.ShoppingCarMapper;
import com.w.wx.service.impls.GoodServiceSImpl;
import com.w.wx.service.impls.UserServiceImpl;
import com.w.wx.utils.ALToHMUtil;
import com.w.wx.utils.GetOpenidUtil;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;


@Slf4j
@RestController
@RequestMapping("/wx/user")
public class UserInfoContro {

    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private GoodServiceSImpl goodService;



    /**
     * 返回用户唯一标识
     * @param appid
     * @param code
     * @param secret
     * @return
     */
    @RequestMapping("/getOpenid")
    public String getWxOpenId(String appid,String code,String secret,HttpServletRequest httpServletRequest){
        if(appid==null||code==null||secret==null){
            return "error";
        }
        String jsonId= GetOpenidUtil.getopenid(appid,code,secret);
        //log.info(jsonId);
        HttpSession session = httpServletRequest.getSession();
        session.setAttribute("openid",jsonId);
        return jsonId;
    }

    /**
     * 用户注册
     * @return
     */
    @PostMapping ("/register")
    public Msg setUserInfo(@RequestBody UserEr userinfo){

        return ALToHMUtil.retrunEasy(userService.setUserInfo(userinfo));
    }

    /**
     * 判断用户是否已注册
     */
    @RequestMapping("/isRegister")
    public Msg isRegister(String openid){

        return userService.isRegister(openid);
    }

    /**
     * 通过openid获取用户信息
     * @param openid
     * @return
     */
    @RequestMapping("/getUserInfo")
    public Msg getUserInfo(String openid){
        //从数据库获取
       UserEr user =  userService.getUserInfo(openid);
       Msg msg = new Msg();
       if(user==null){
           msg.setCode(100);
           msg.setMsg("处理失败");
       }else{
           msg.setCode(200);
           msg.setMsg("处理成功");
           msg.add("userinfo",user);
       }
        return msg;
    }

    /**
     * 新增或者更新用户详细信息
     * @param userDetailInfo
     * @return
     */
    @RequestMapping("/updateUserInfo")
    public Msg updataUserInfo(@RequestBody UserDe userDetailInfo){
      return ALToHMUtil.retrunEasy(userService.updateUserInfo(userDetailInfo));
    }

    /**
     * 获取用户详细信息
     * @param openid
     * @return
     */
    @RequestMapping("/getUserDeInfo")
    public Msg getUserDeInfo(String openid){
        UserDe b =  userService.getUserDeInfo(openid);
        if(b!=null){
            Msg msg =new Msg();
            msg.setMsg("处理成功");
            msg.setCode(200);
            msg.add("userDe",b);
            return msg;
        }else{
            return Msg.fail();
        }
    }

    //--------------------购物车----------------------------------

    /**
     * 购物车添加信息
     * @param openid
     * @param gid
     * @return
     */
    @RequestMapping("/addGooods")
    public Msg addGoodsInCar(String openid,Integer gid){
      return ALToHMUtil.retrunEasy(userService.addGoodInCar(openid,gid));
    }

    @PostMapping("/addGoodsToCar")
    public Msg addGoodsToCar(@RequestBody JSONObject sums){

        return ALToHMUtil.retrunEasy(userService.addGoodsInCar(sums));
    }

    /**
     * 获取购物车物品封面信息
     * @param openid
     * @return
     */
    @RequestMapping("/getMyCarSimpleInfo")
    public Msg getMyCarSimpleInfo(String openid){
        ArrayList<Goods> goods =  userService.getMyCarSimpleInfo(openid);
        return ALToHMUtil.listTomap(goods);
    }

    /**
     * 查询出用户收藏物品gid
     * @param openid
     * @return
     */
    @RequestMapping("/getFavorites")
    public Msg getFavoritesByOpenid(String openid){
        ArrayList<Integer> favorites = userService.getFavoritesByOpenid(openid);
        return Msg.success().add("favorites",favorites);
    }
    /**
     * 删除car表信息
     * @param gid
     * @return
     */
    @RequestMapping("/deleteCarGoods")
    public Msg deleteCarGoods(Integer gid){
       return  ALToHMUtil.retrunEasy(goodService.deleteCarGoods(gid));
    }

    /**
     * 删除我的发布物品信息
     * @param gid
     * @return
     */
    @GetMapping("/deleteMyIssue")
    public Msg deleteGoods(Integer gid){
       return ALToHMUtil.retrunEasy(goodService.deleteGoods(gid));
    }

    /**
     * 删除审核信息
     * @param gid
     * @param state
     * @return
     */
    @RequestMapping("/deleteCheckFail")
    public Msg deleteCheckFail(int gid,int state){
        if(state!=1){
            return ALToHMUtil.retrunEasy(goodService.deleteGoods(gid));
        }
        return Msg.success();
    }

    /**
     * 返回审核不通过原因
     * @param gid
     * @return
     */
    @RequestMapping("/whyNot")
    public Msg whyNotGo(Integer gid){
        Notice notice = goodService.whyNotGo(gid);
        if(notice!=null){
            Msg msg = new Msg();
            msg.setCode(200);
            msg.setMsg("处理成功");
            msg.add("notice",notice);
            return msg;
        }else {
            return Msg.fail();
        }
    }
}